package com.hui.utils.nettydemo.client;

import java.awt.Button;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

public class NettyClient {
    
    
		public static Map<String, ChannelFuture> map = new HashMap<>();
        public static void main(String[] args) throws Exception {
        	 
        	
        	EventLoopGroup group = new NioEventLoopGroup();
             try {
            	 JFrame wind = new JFrame();
            	 wind.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出应用程序
                 wind.setTitle("netty demo v1.0");
                 wind.setSize(500, 300);
                

                 JPanel  contentPane=new  JPanel();
                 
                 JButton button= new JButton("send");
                 JTextField text1 = new JTextField(10);

                 contentPane.add(button);
                 button.addMouseListener(new MouseListener() {
					
					@Override
					public void mouseReleased(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mousePressed(MouseEvent e) {
						
					}
					
					@Override
					public void mouseExited(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseEntered(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseClicked(MouseEvent e) {
						String text = text1.getText();
						System.out.println(">>>>>>>>>>>>"+text);
						ChannelFuture f = map.get("111");
						f.channel().writeAndFlush("我是客户端 hello Service!"+Thread.currentThread().getName()+":--->:");
			                 
						
					}
				});
               
                 
                
                 contentPane.add(text1);

                 wind.setContentPane(contentPane);
                 
                 wind.setVisible(true);
                 Bootstrap b = new Bootstrap();
                 b.group(group);
                 b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
                 b.handler(new ChannelInitializer<SocketChannel>() {
                     @Override
                     protected void initChannel(SocketChannel ch) throws Exception {
                         ChannelPipeline pipeline = ch.pipeline();
                         pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                         pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
                         pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
                         pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));

                         pipeline.addLast("handler", new HelloClient());
                     }
                 });
                 
                 ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
                 map.put("111", f);
                 
                 
                 f.channel().writeAndFlush("我是客户端 hello Service!"+Thread.currentThread().getName()+":--->:");
                 //f.channel().closeFuture().sync();
                 f.sync();
                
                 
             } catch (Exception e) {

             } finally {
                // group.shutdownGracefully();
             }
        }
}